﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace FileBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeTreeView();
        }
        private void InitializeTreeView()
        {
            // 添加 C: 
            TreeNode rootNode = new TreeNode("C:");
            rootNode.Tag = "C:\\"; // 存储路径信息
            treeView.Nodes.Add(rootNode);
            // 添加展开节点的事件处理程序
            treeView.BeforeExpand += TreeView_BeforeExpand;
            // 填充 C:下的子文件夹
            PopulateTreeView(rootNode, "C:\\");
        }
        private void UpdatePathLabel(string path)
        {
            toolStripLabelPath.Text = "当前路径: " + path;
        }

        private void PopulateTreeView(TreeNode parentNode, string path)
        {
            try
            {
                string[] directories = Directory.GetDirectories(path);
                foreach (string directory in directories)
                {
                    TreeNode node = new TreeNode(Path.GetFileName(directory));
                    node.Tag = directory;
                    parentNode.Nodes.Add(node);
                    node.Nodes.Add("");
                }
            }
            catch (UnauthorizedAccessException)
            {
                // 权限不足异常
            }
            catch (DirectoryNotFoundException)
            {
                // 文件夹不存在异常
            }
        }

        private void TreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            // 清除虚拟节点
            e.Node.Nodes.Clear();
            // 获取该节点对应的文件夹路径
            string selectedPath = e.Node.Tag.ToString();
            // 填充下一级子文件夹
            PopulateTreeView(e.Node, selectedPath);
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string selectedPath = e.Node.Tag.ToString();
            PopulateListView(selectedPath);
            UpdatePathLabel(selectedPath);
        }

        private void PopulateListView(string directoryPath)
        {
            try
            {
                listView.Items.Clear();
                string[] directories = Directory.GetDirectories(directoryPath);
                string[] files = Directory.GetFiles(directoryPath);

                foreach (string directory in directories)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(directory));
                    item.SubItems.Add("文件夹");
                    listView.Items.Add(item);
                }

                foreach (string file in files)
                {
                    string extension = Path.GetExtension(file).ToLower();
                    ListViewItem item = new ListViewItem(Path.GetFileName(file));
                    item.SubItems.Add(extension);
                    listView.Items.Add(item);
                }
            }
            catch (UnauthorizedAccessException)
            {
                // 权限不足异常
            }
            catch (DirectoryNotFoundException)
            {
                // 文件夹不存在异常
            }
        }

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView.SelectedItems.Count > 0)
            {
                string selectedItem = listView.SelectedItems[0].Text;
                string selectedPath = Path.Combine(treeView.SelectedNode.Tag.ToString(), selectedItem);
                string extension = Path.GetExtension(selectedPath).ToLower();

                if (extension == ".exe")
                {
                    Process.Start(selectedPath);
                }
                else if (extension == ".txt")
                {
                    Process.Start("notepad.exe", selectedPath);
                }
                else if (Directory.Exists(selectedPath)) // 如果是文件夹
                {
                    // 打开文件夹并展开相应的节点
                    TreeNode node = new TreeNode(selectedItem);
                    node.Tag = selectedPath;
                    treeView.SelectedNode.Nodes.Add(node);
                    treeView.SelectedNode.Expand();
                    // 更新右边的ListView
                    PopulateListView(selectedPath);
                }
                UpdatePathLabel(selectedPath);
            }
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Title = "选择要打开的文件";
                openFileDialog.Filter = "所有文件|*.*";
                openFileDialog.CheckFileExists = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string selectedFile = openFileDialog.FileName;
                    // 处理选中的文件，可以根据文件类型执行不同操作
                    string extension = Path.GetExtension(selectedFile).ToLower();
                    if (extension == ".exe")
                    {
                        Process.Start(selectedFile);
                    }
                    else if (extension == ".txt")
                    {
                        Process.Start("notepad.exe", selectedFile);
                    }
                }
            }
        }
    }
}
